<include file="public@header"/>
<body>
<div class="wrap">

    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">编辑班序</h3>
        </div>
        <div class="panel-body">
            <form action="" method="post">
                <textarea name="prologue" id="editor1" rows="10" cols="80">{$prologue}</textarea>
                <button class="btn btn-default" type="submit" style="margin-top:20px;">保存</button>
            </form>
        </div>
    </div>
</div>
<script language="javascript" type="text/javascript" src="__ROOT__/assets/src/js/ckeditor/ckeditor.js"></script>
<script language="javascript" type="text/javascript">
    CKEDITOR.config.filebrowserImageUploadUrl = "{:url('admin/prologue/uploadImg')}";
    var editor = CKEDITOR.replace('editor1');
    function uplaodImage() {
        editor.on('change', function (e) {
            var a = e.editor.document;
            var b = a.find("img");
            var count = b.count();
            for(var i=0;i<count;i++){
                var src =b.getItem(i).$.src;//获取img的src
                console.log(src)
                if(src.substring(0,10)=='data:image'){ //判断是否是二进制图像，是才处理
                    var img1=src.split(',')[1];
                    var img2=window.atob(img1);
                    alert('img2 size='+img2.length);
                    var ia = new Uint8Array(img2.length);
                    for (var x = 0; x < img2.length; x++) {
                        ia[x] = img2.charCodeAt(x);
                    };
                    //获得图片的类型
                    var w1=src.indexOf(":");//获得指定字符的第一个下标值
                    var w2=src.indexOf(";");
                    var imgType= src.substring(w1+1, w2);//返回一个包含从 start 到最后（不包含 end ）的子字符串的字符串

                    var blob=new Blob([ia], {type:imgType});
                    var formdata=new FormData();
                    formdata.append('croppedImage',blob);

                    $.ajax({
                        type:"POST",
                        url:'{:url("admin/prologue/uploadImg")}',//服务器url
                        async:false,//同步，因为修改编辑器内容的时候会多次调用change方法，所以要同步，否则会多次调用后台
                        data:formdata,
                        processData: false,
                        contentType: false,
                        success:function(json){
                            var imgurl=json.url; //获取回传的图片url
                            //alert('返回的url='+imgurl);

                            //获取并更改到现有的图片标签src的值
                            b.getItem(i).$.src=imgurl;
                            var a = CKEDITOR.instances.content.document.$.getElementsByTagName("img")[i]; //content为textarea的id
                            a.setAttribute('data-cke-saved-src',imgurl);
                        }
                    });
                }
            }
        });
    }
</script>
</body>
</html>